<?php

namespace app\index\controller;

use app\index\model\Menu;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
        $model = Menu::get(1);
        $model->name = "333";
        echo $model->save();
        echo '<br>';
        echo $model->getLastSql();
    }

    public function add($name)
    {
//        $model = new Menu();
//        $model->data([
//            "name" => $name
//        ]);
//        $res = $model->save();
        $res = Menu::create([
            "name" => $name
        ]);
        if ($res) {
            return json([
                "code" => 200,
                "msg" => "新增成功",
                "obj" => $res,
            ]);
        } else {
            return json([
                "code" => 201,
                "msg" => "新增失败",
            ]);
        }
    }

    public function update()
    {
        $query = input();
        $model = Menu::get($query["id"]);
        $res = $model->save([
            "name" => $query["name"]
        ]);
        if ($res) {
            return ajaxReturn(200, "更新成功");
        } else {
            return ajaxReturn(201, "更新失败");
        }
    }

    public function delete()
    {
        $query = input();
        $ids = $query["ids"];
        $model = Menu::get($ids);
        $res = $model->save([
            "is_delete" => 1,
        ]);
        if ($res) {
            return ajaxReturn(200, "删除成功");
        } else {
            return ajaxReturn(201, "删除失败");
        }

    }
}
